package vn.gq.tym.fragment;

import java.util.List;

import vn.gq.tym.model.CategoryAdapter;
import vn.gq.tym.storytemplate.R;
import vn.gq.tym.widget.tab.BaseFragment;
import vn.gq.udv.db.items.Category;
import vn.gq.udv.utils.AdmobAdsHandler;
import vn.gq.udv.utils.Bookmark;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.ads.AdView;

public class CategoryFragment extends BaseFragment {

	private ListView mListView;
	private TextView chapterNotification;
	private Bookmark bookmark;
	private AdmobAdsHandler mAdHandler;
	private ProgressBar mProgressBar;
	private CategoryAdapter mAdapter;
	private List<Category> mCategories;
	private static final String TAG = "CategoryFragment";
	
	public static CategoryFragment newInstance() {
		CategoryFragment f = new CategoryFragment();
		return f;
	}
	
	@Override
	public int getContainerId() {
		return R.id.main_container;
	}

	@Override
	public int getLayoutId() {
		return R.layout.chapter_scr;
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void createView() {
		mListView = (ListView) mLayout.findViewById(R.id.chapterlist);
		mListView.setVisibility(View.GONE);
		mProgressBar = (ProgressBar) mLayout.findViewById(R.id.progress);
		mProgressBar.setVisibility(View.VISIBLE);
		chapterNotification = (TextView) mLayout.findViewById(R.id.tvnotification);
		chapterNotification.setSelected(true);
		bookmark = new Bookmark(getActivity(), chapterNotification);
		mAdapter = new CategoryAdapter(getActivity());
		mAdapter.load(new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mListView.setVisibility(View.VISIBLE);
				mProgressBar.setVisibility(View.GONE);
				mCategories = mAdapter.getData();
			}
		});
		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (id != -1) {
					Category cat = mCategories.get(position);
					bookmark.setCategoryBookmark(cat);
					ChapterFragment f = ChapterFragment.newInstance(cat.getCatId(), cat.getCatName());
					startFragment(f.getContainerId(), f, TAG);
				}
			}
		});
		mAdHandler = new AdmobAdsHandler((AdView) findViewById(R.id.adView));
		
	}
}
